﻿
Imports System.Data
Imports System.Data.SqlClient


''' <summary>
''' 車倆設定/區域設置
''' </summary>
''' <remarks>
''' <list type="Create Person">huyang</list>
''' <list type="Create Date">2007-07-10</list>
''' <list type="Modifies Date">2007-12-18</list>
''' <list type="Modifies Remark">把區域設置搬過來了</list>
''' </remarks>
Public Class Frm_Truct

    Implements IDServer.IDServer.IDServer

#Region "Variabled Define"

#Region "System Variabled"


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property


#End Region

#Region "User Variabled"

    ''' <summary>
    ''' 窗體是否已加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoodBool As Boolean = False

    ''' <summary>
    ''' 區域數據表
    ''' </summary>
    ''' <remarks></remarks>
    Private Dtab_Area As New DataTable

    ''' <summary>
    ''' 車倆數據表
    ''' </summary>
    ''' <remarks></remarks>
    Private Dtab_Truck As New DataTable

    ''' <summary>
    ''' 貨品類別表
    ''' </summary>
    ''' <remarks></remarks>
    Private Dtab_GoodsType As New DataTable


    ''' <summary>
    ''' 是否已選擇
    ''' </summary>
    ''' <remarks></remarks>
    Private fSelectBool As Boolean = False



#End Region


#End Region

#Region "Methods Define"

#Region "Initialize"

    ''' <summary>
    ''' 初始化數據集
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Initialize()
        Try

            Dim Dset As DataSet = Me.Re_InitDataSet()
            Dtab_Area = Dset.Tables(0)
            Dtab_Truck = Dset.Tables(1)
            Dtab_GoodsType = Dset.Tables(2)
            Load_TreeView(Me.Tvw_1, "全部", False, True)
            Load_TreeView(Me.Tvw_Area, "全部", False, True)
            Me.AddHandlers(New Control() {Me.Pan_2_1, Me.GroupBox3})
            FillCombox()
            SetEnabled_Tab1(False)
            SetEnabled(False)
        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    ''' <summary>
    ''' 返回初始化數據集
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Re_InitDataSet() As DataSet
        Dim Dset As New DataSet
        Try
            Dim Sql As String = ""

            Sql &= " select * from BSC_AreaSet  order by AreaNum " & vbCrLf
            Sql &= " select * from BSC_Truck " & vbCrLf
            Sql &= " select Distinct fCode,fCName from BSC_GoodsType where fDefine=0" & vbCrLf

            Dset = Exec(Sql, E_ExecType.DataSet)


        Catch ex As Exception
            MessageBox.Show("Re_InitDataSet:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return Dset
    End Function

    ''' <summary>
    ''' 填充下拉列表
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub FillCombox()

        For Each Item As String In [Enum].GetNames(GetType(E_WeekDay))
            Me.cbxDependDate.Items.Add(Item)
        Next
        Me.cbxDependDate.Text = ""

        With Me.cbxDependDept
            .DataSource = Me.Dtab_GoodsType
            .ValueMember = "fCode"
            .DisplayMember = "fCName"
        End With

        cbxDependDept.Text = ""

    End Sub


#End Region

#Region "TreeView Events"

#Region "TreeView Mehtod"

    ''' <summary>
    ''' 加載圖片
    ''' </summary>
    ''' <param name="Node"></param>
    ''' <param name="Level"></param>
    ''' <remarks></remarks>
    Private Sub LoadNodeImage(ByVal Node As TreeNode, ByVal Level As Integer)
        Select Case Level
            Case 0
                Node.ImageIndex = 1
                Node.SelectedImageIndex = 2
            Case 1
                Node.ImageIndex = 1
                Node.SelectedImageIndex = 2
            Case 2
                Node.ImageIndex = 1
                Node.SelectedImageIndex = 2
            Case 3
                Node.ImageIndex = 1
                Node.SelectedImageIndex = 2
            Case 4
                Node.ImageIndex = 1
                Node.SelectedImageIndex = 2
            Case Else
                Node.ImageIndex = 1
                Node.SelectedImageIndex = 2
        End Select
    End Sub

    ''' <summary>
    ''' 加載樹結點
    ''' </summary>
    ''' <param name="Node"></param>
    ''' <remarks></remarks>
    Private Sub Load_TreeNode(ByRef Node As TreeNode)
        Try
            If Node.Nodes.Count > 1 OrElse Node.Nodes(0).Text.Trim <> "..." Then Exit Sub
            Node.Nodes.Clear()
            Select Case Node.Level
                Case 0
                    ''加載線路
                    Dim DRow() As DataRow = Me.Dtab_Area.Select("ParentId=0", "AreaNum")
                    For Each Row As DataRow In DRow
                        Dim AreaCode As String = Row("AreaCode") & ""
                        Dim AreaName As String = Row("AreaName") & ""
                        Dim Tnode As TreeNode = Node.Nodes.Add(AreaName)
                        Tnode.Tag = AreaCode
                        LoadNodeImage(Tnode, 1)
                        Dim PRow() As DataRow = Me.Dtab_Truck.Select("ParentId='" & AreaCode & "'")
                        If PRow.Length > 0 Then
                            Tnode.Nodes.Add("...")
                        End If
                    Next
                Case 1

                    Dim AreaCode As String = Node.Tag & ""

                    Dim DRow() As DataRow = Me.Dtab_Truck.Select("ParentId='" & AreaCode & "'")

                    For Each Row As DataRow In DRow
                        Dim TruckId As String = Row("TruckId") & ""
                        Dim TruckName As String = Row("TruckName") & ""
                        Dim Tnode As TreeNode = Node.Nodes.Add(TruckName)
                        Tnode.Tag = AreaCode & ":" & TruckId
                        LoadNodeImage(Tnode, 2)
                    Next

            End Select
        Catch ex As Exception
            MessageBox.Show("Load_TreeNode:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    ''' <summary>
    ''' 加載區域
    ''' </summary>
    ''' <param name="Node"></param>
    ''' <remarks></remarks>
    Private Sub Load_TreeNodeForAreaSet(ByRef Node As TreeNode)
        Try
            If Node.Nodes.Count > 1 OrElse Node.Nodes(0).Text.Trim <> "..." Then Exit Sub
            Node.Nodes.Clear()

            Dim ParentId As String = Node.Tag.ToString.Trim

            Dim DRow() As DataRow = Me.Dtab_Area.Select("ParentId='" & ParentId & "'", "AreaNum")
            For Each Row As DataRow In DRow
                Dim NodeText As String = Row("AreaName") & ""
                Dim NodeKey As String = Row("Id") & ""
                Dim ChildNode As TreeNode = Node.Nodes.Add(NodeText)
                ChildNode.Tag = NodeKey
                Dim CRow() As DataRow = Me.Dtab_Area.Select("ParentId='" & NodeKey & "'", "AreaNum")
                If CRow.Length > 0 Then
                    ChildNode.Nodes.Add("...")
                End If
                LoadNodeImage(ChildNode, Node.Level + 1)
            Next

        Catch ex As Exception
            MessageBox.Show("Load_TreeNodeForAreaSet:")
        End Try
    End Sub

    ''' <summary>
    ''' 刪除樹結點
    ''' </summary>
    ''' <param name="Tvw">樹</param>
    ''' <param name="SelectNode">要刪除的結點</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Delete_TreeNode_AreaSet(ByRef Tvw As TreeView, ByVal SelectNode As TreeNode) As Boolean
        Try

            If SelectNode Is Nothing Then Return False

            If Not SelectNode Is Nothing AndAlso SelectNode.Nodes.Count > 0 Then
                MessageBox.Show("些結點下還有子結點不能刪除。", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Return False
            End If

            If SelectNode.Level = 1 Then
                If MessageBox.Show(My.Resources.Deleteline, Msg.CompanyName, MessageBoxButtons.OKCancel) <> DialogResult.OK Then
                    Return False
                End If
            Else
                If MessageBox.Show(My.Resources.ConfirmDelete, Msg.CompanyName, MessageBoxButtons.OKCancel) <> DialogResult.OK Then
                    Return False
                End If
            End If

            Dim ParentNode As TreeNode = SelectNode.Parent
            Dim NextNode As TreeNode = SelectNode.NextNode

            Dim PrevNode As TreeNode = SelectNode.PrevNode

            Dim Id As Integer = Val(SelectNode.Tag & "")

            Dim sql As String = ""

            If SelectNode.Level = 1 Then
                sql &= " delete dbo.BSC_Truck" & vbCrLf
                sql &= " where ParentId=(select top 1 AreaCode from dbo.BSC_AreaSet where Id=" & Id & ")" & vbCrLf
            End If
            sql &= " delete BSC_AreaSet where Id=" & Id & ""

            Exec(sql, E_ExecType.ExecuteNonQuery)

            Dim DRow() As DataRow = Me.Dtab_Area.Select("Id=" & Id & "")

            For Each Row As DataRow In DRow
                Row.Delete()
            Next

            Me.Dtab_Area.AcceptChanges()

            SelectNode.Remove()

            If Not NextNode Is Nothing Then
                Tvw.SelectedNode = NextNode
            ElseIf Not PrevNode Is Nothing Then
                Tvw.SelectedNode = PrevNode
            ElseIf Not ParentNode Is Nothing Then
                Tvw.SelectedNode = ParentNode
            End If

            MessageBox.Show(My.resources.DeleteSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception
            MessageBox.Show("Delete_TreeNode_AreaSet:" & ex.Message & ex.Source, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function



#End Region

#End Region

#Region "添加事件"

    ''' <summary>
    ''' KeyDown 事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Text_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            Select Case e.KeyCode
                Case Keys.Enter
                    If e.Shift = True Then
                        If TypeOf sender Is ComboBox AndAlso CType(sender, ComboBox).DroppedDown = True Then
                            CType(sender, ComboBox).DroppedDown = False
                        End If
                        SendKeys.Send("+{Tab}")
                        SendKeys.Send("{F4}")
                    Else
                        If TypeOf sender Is ComboBox AndAlso CType(sender, ComboBox).DroppedDown = True Then
                            CType(sender, ComboBox).DroppedDown = False
                        End If
                        SendKeys.Send("{Tab}")
                        SendKeys.Send("{F4}")
                    End If
                Case Else

            End Select
        Catch ex As Exception
            MessageBox.Show("Text_KeyDown:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    ''' <summary>
    ''' 添加事件
    ''' </summary>
    ''' <param name="Pans"></param>
    ''' <remarks></remarks>
    Private Sub AddHandlers(ByVal Pans() As Control)
        Try
            For Each Pan As Control In Pans
                For Each Ctrl As Control In Pan.Controls
                    If TypeOf Ctrl Is TextBox _
                       OrElse TypeOf Ctrl Is ComboBox _
                       OrElse TypeOf Ctrl Is NumericUpDown _
                       OrElse TypeOf Ctrl Is MaskedTextBox Then
                        AddHandler Ctrl.KeyDown, AddressOf Text_KeyDown
                    End If
                Next
            Next
        Catch ex As Exception
            MessageBox.Show("AddHandlers:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "控制事件"

    ''' <summary>
    ''' 設置控件是否可用
    ''' </summary>
    ''' <param name="bEnabled"></param>
    ''' <remarks></remarks>
    Private Sub SetEnabled(ByVal bEnabled As Boolean)
        Try
            Dim bReadOnly As Boolean = Not bEnabled


            For Each Ctrl As Control In Me.Pan_2_1.Controls
                If TypeOf Ctrl Is TextBox Then
                    DirectCast(Ctrl, TextBox).ReadOnly = bReadOnly
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("SetEnabled:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub SetEnabled_Tab1(ByVal bEnabled As Boolean)

        Dim bReadOnly As Boolean = Not bEnabled

        For Each Ctrl As Control In GroupBox3.Controls
            If TypeOf Ctrl Is TextBox Then
                DirectCast(Ctrl, TextBox).ReadOnly = bReadOnly
            End If
            If TypeOf Ctrl Is ComboBox Then
                DirectCast(Ctrl, ComboBox).Enabled = bEnabled
            End If

        Next

    End Sub

    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type">操作類型</param>
    ''' <param name="IsFind">是否是流覽</param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Dim Node As TreeNode = Nothing

            Select Case Me.TabControl1.SelectedIndex
                Case 0
                    Node = Me.Tvw_Area.SelectedNode
                Case 1
                    Node = Me.Tvw_1.SelectedNode
                Case Else
                    '' Node = Me.Tvw_Area.SelectedNode
            End Select

            If Node Is Nothing Then Exit Sub

            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        Select Case Me.TabControl1.SelectedIndex
                            Case 0
                                .ToolStripAdd.Enabled = True
                                .ToolStripEdit.Enabled = IsFind AndAlso Node.Level > 0
                                .ToolStripDelete.Enabled = IsFind AndAlso Node.Level > 0 AndAlso (Node.Nodes.Count = 0)
                            Case 1
                                .ToolStripAdd.Enabled = True AndAlso Node.Level = 1
                                .ToolStripEdit.Enabled = IsFind AndAlso Node.Level = 2
                                .ToolStripDelete.Enabled = IsFind AndAlso Node.Level = 2
                        End Select

                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

#End Region

#Region "FillDetail"

    ''' <summary>
    ''' 填充明細
    ''' </summary>
    ''' <param name="Node">選擇結點</param>
    ''' <remarks></remarks>
    Private Sub FillDetail(ByVal Node As TreeNode)
        Try
            Dim TruckId As String = ""
            Dim ParentId As String = ""
            If Node.Level = 2 Then
                ParentId = Node.Tag.ToString.Split(":")(0).Trim
                TruckId = Node.Tag.ToString.Split(":")(1).Trim
            End If
            Dim DRow() As DataRow = Me.Dtab_Truck.Select("Isnull(ParentId,'')='" & ParentId & "'  and Isnull(TruckId,'')='" & TruckId & "'")
            If DRow.Length > 0 Then
                Me.txtTruckId.Text = DRow(0).Item("TruckId") & ""
                Me.txtTruckName.Text = DRow(0).Item("TruckName") & ""
                Me.txtRemark.Text = DRow(0).Item("Remark") & ""
                Me.txtfWeight.Text = Val(DRow(0).Item("fWeight") & "").ToString("0.00")
                Me.txtfVolume.Text = Val(DRow(0).Item("fVolume") & "").ToString("0.00")
            Else
                Me.txtTruckId.Text = ""
                Me.txtTruckName.Text = ""
                Me.txtRemark.Text = ""
                Me.txtfVolume.Text = ""
                Me.txtfWeight.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show("FillDetail:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    ''' <summary>
    ''' 填充區域設置
    ''' </summary>
    ''' <param name="node"></param>
    ''' <remarks></remarks>
    Private Sub FillDetialAreaSet(ByVal node As TreeNode)

        Dim DependDept As String = ""
        Dim DependDate As String = ""

        If node Is Nothing Then
            GoTo A
        End If

        Dim Rows() As DataRow = Me.Dtab_Area.Select("ID=" & Val(node.Tag & "") & "")

        If Rows.Length > 0 Then

            Me.AreaCode.Text = Rows(0).Item("AreaCode") & ""
            Me.AreaName.Text = Rows(0).Item("AreaName") & ""
            Me.AreaNum.Text = Rows(0).Item("AreaNum") & ""
            Me.AreaAddress.Text = Rows(0).Item("AreaAddress") & ""
            Me.AreaDistance.Text = Rows(0).Item("AreaDistance") & ""
            DependDept = Rows(0).Item("DependDept") & ""
            Me.txtSummaryDate.Text = Rows(0).Item("SummaryDate") & ""
            DependDate = Rows(0).Item("DependDate") & ""

            Me.TreeView2.Nodes.Clear()
            Me.TreeView3.Nodes.Clear()
            If DependDept.Length > 0 Then
                Dim A() As String = DependDept.Split(",")
                For Each item As String In A
                    Dim DRow() As DataRow = Dtab_GoodsType.Select("fCode='" & item & "'")
                    If DRow.Length > 0 Then
                        Dim fName As String = DRow(0).Item("fCName")
                        Dim nodeTemp As TreeNode = Me.TreeView2.Nodes.Add(item, fName)
                        nodeTemp.Tag = item
                    End If
                Next
            End If


            If DependDate.Length > 0 Then
                Dim A() As String = DependDate.Split(",")
                For Each item As String In A
                    Dim Weekday As Integer = Val(item)
                    If Weekday <= 7 AndAlso Weekday > 0 Then
                        Dim eitem As E_WeekDay = Weekday
                        Dim NodeTemp As TreeNode = Me.TreeView3.Nodes.Add(Weekday, eitem.ToString)
                        NodeTemp.Tag = Weekday
                    End If
                Next
            End If
        Else
A:

            Me.AreaCode.Text = ""
            Me.AreaName.Text = ""
            Me.AreaNum.Text = ""
            Me.AreaAddress.Text = ""
            Me.AreaDistance.Text = ""

            Me.txtSummaryDate.Text = ""

            Me.TreeView2.Nodes.Clear()
            Me.TreeView3.Nodes.Clear()

        End If

        Me.cbxDependDept.Text = ""
        Me.cbxDependDate.Text = ""

    End Sub


#End Region

#Region "Data Detail"

    Private Function btnSaveData() As Boolean
        Dim Bool As Boolean = True
        Try
            If Validated() = False Then Return False
            Dim sql As String = ""
            Dim Node As TreeNode = Me.Tvw_1.SelectedNode

            ''Eastop中的車輛編號,對應Tingpoo系統的BSC_Areaset中的AreaCode（ BSC_Truck中的parentid）
            Dim ParentId As String = Node.Tag.ToString.Split(":")(0).Trim
            Dim TruckId As String = Me.txtTruckId.Text.Trim & ""

            Dim TruckName As String = Me.txtTruckName.Text.Trim & ""
            Dim Remark As String = Me.txtRemark.Text.Trim & ""
            Dim fVolume As Double = Val(Me.txtfVolume.Text)
            Dim fWeight As Double = Val(Me.txtfWeight.Text)

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                sql &= "  Insert BSC_Truck(ParentId,TruckId,TruckName,Remark,fVolume,fWeight)" & vbCrLf
                sql &= "  values('" & ParentId & "','" & TruckId & "','" & TruckName & "','" & Remark & "'," & fVolume & "," & fWeight & ") " & vbCrLf
            Else
                sql &= "  Update BSC_Truck set TruckName='" & TruckName & "' ,Remark='" & Remark & "',fVolume=" & fVolume & " ,fWeight=" & fWeight & " where TruckId='" & TruckId & "' and ParentId='" & ParentId & "' " & vbCrLf
            End If

            Exec(sql, E_ExecType.ExecuteNonQuery)

            UpdateData(ParentId, TruckId)

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                Dim Tnode As TreeNode = Node.Nodes.Add(TruckName)
                Tnode.Tag = ParentId & ":" & TruckId
                Dim TempA As IDServer.IDServer.ActionBtn = ActionHelp
                ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
                fLoodBool = False
                Me.Tvw_1.SelectedNode = Tnode
                fLoodBool = True
                ActionHelp = TempA
                Me.LoadNodeImage(Tnode, 2)
                If Node.IsExpanded = False Then
                    Node.Expand()
                End If
            Else
                Node.Text = TruckName
            End If
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                MessageBox.Show(My.Resources.AddSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show(My.resources.EditSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            Me.SetButton(ButtonType.Browse, True)
            Me.SetEnabled(False)
        Catch ex As Exception
            MessageBox.Show("btnSaveData:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Bool = False
        End Try
        Return Bool
    End Function

    ''' <summary>
    ''' 驗證
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Validated() As Boolean
        Try

            Dim Node As TreeNode = Me.Tvw_1.SelectedNode

            'If Node.Level <> 2 Then
            '    MessageBox.Show("只能編輯車次。", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            '    Return False
            'End If

            If Me.txtTruckId.Text.Trim = "" Then
                MessageBox.Show(my.Resources.NotNullfCode, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Me.txtTruckId.Focus()
                Me.txtTruckId.SelectAll()
                Return False
            End If

            If Me.txtTruckName.Text.Trim = "" Then
                Me.txtTruckName.Focus()
                Me.txtTruckName.SelectAll()
                MessageBox.Show(My.Resources.namenotnulll, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End If

            Dim fMsgErr As String = ""
            Dim Bool As Boolean = Me.IsExistsBool(fMsgErr)
            If Bool = True Then
                MessageBox.Show(fMsgErr, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End If

            Return True
        Catch ex As Exception
            MessageBox.Show("Validated:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    ''' <summary>
    ''' Judgement Is Exists
    ''' </summary>
    ''' <param name="fMsgErr"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function IsExistsBool(ByRef fMsgErr As String) As Boolean
        Dim Bool As Boolean = False
        Try

            Dim sql As String = ""
            sql &= " select Count(*) as mCount from BSC_Truck where TruckId='" & Me.txtTruckId.Text.Trim & "' " & vbCrLf
            Dim mCount As Integer = 0
            mCount = Val(Exec(sql, E_ExecType.ExecuteScalar) & "")
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                If mCount > 0 Then
                    Bool = True
                    fMsgErr = My.Resources.Codeexists
                End If
            Else
                If mCount <= 0 Then
                    Bool = True
                    fMsgErr = My.Resources.Recorddelete
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("IsExistsBool:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return Bool

    End Function

    ''' <summary>
    ''' Update Data
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function UpdateData(ByVal ParentId As String, ByVal TruckId As String) As Boolean
        Dim bool As Boolean = True
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                Dim Row As DataRow = Me.Dtab_Truck.NewRow
                Row("ParentId") = ParentId
                Row("TruckId") = TruckId
                Row("TruckName") = Me.txtTruckName.Text.Trim
                Row("Remark") = Me.txtRemark.Text.Trim & ""
                Row("fWeight") = Val(Me.txtfWeight.Text)
                Row("fVolume") = Val(Me.txtfVolume.Text)

                Me.Dtab_Truck.Rows.Add(Row)
            Else
                Dim DRow As DataRow() = Me.Dtab_Truck.Select("Isnull(ParentId,'')='" & ParentId & "' and Isnull(TruckId,'')='" & TruckId & "'")
                If DRow.Length > 0 Then
                    DRow(0).Item("TruckName") = Me.txtTruckName.Text.Trim & ""
                    DRow(0).Item("Remark") = Me.txtRemark.Text.Trim & ""
                    DRow(0).Item("fWeight") = Val(Me.txtfWeight.Text)
                    DRow(0).Item("fVolume") = Val(Me.txtfVolume.Text)
                End If
            End If
            Me.Dtab_Truck.AcceptChanges()
        Catch ex As Exception
            MessageBox.Show("UpdateData:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return bool
    End Function

    ''' <summary>
    ''' 區域保存
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SaveDataAreaSet() As Boolean
        Dim Bool As Boolean = False
        Try
            If Validated_AreaSet() = False Then Return False
            Dim sql As String = ""

            Dim AreaCode As String = Me.AreaCode.Text
            Dim AreaName As String = Me.AreaName.Text
            Dim AreaNum As String = Me.AreaNum.Text
            Dim AreaAddress As String = Me.AreaAddress.Text
            Dim AreaDistance As String = Me.AreaDistance.Text
            Dim FullPath As String
            Dim ParentId As Integer
            Dim ID As Integer
            If Me.Tvw_Area.SelectedNode.Tag.ToString = "All" Then
                ParentId = 0
                FullPath = ""
            Else
                ParentId = Me.Tvw_Area.SelectedNode.Tag
                FullPath = Me.Tvw_Area.SelectedNode.FullPath.Replace("全部\", "")
            End If

            Dim DependDept As String = ""
            Dim SummaryDept As String = ""

            Dim TextBuild As New System.Text.StringBuilder
            For Each Node As TreeNode In Me.TreeView2.Nodes
                If TextBuild.ToString <> "" Then
                    TextBuild.Append(",")
                    SummaryDept += ","
                End If
                TextBuild.Append(Node.Tag)
                SummaryDept += Node.Text.Trim
            Next
            DependDept = TextBuild.ToString & ""
            TextBuild.Remove(0, TextBuild.Length)
            Dim DependDate As String = ""

            For Each Node As TreeNode In Me.TreeView3.Nodes
                If TextBuild.ToString <> "" Then
                    TextBuild.Append(",")
                End If
                TextBuild.Append(Node.Tag)
            Next
            DependDate = TextBuild.ToString & ""
            Dim SummaryDate As String = Me.txtSummaryDate.Text.Trim
            Dim SelectNode As TreeNode = Me.Tvw_Area.SelectedNode

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then

                sql = "Insert Into BSC_AreaSet(ParentID,AreaCode,AreaName,AreaNum,AreaAddress,AreaDistance,FullPath,DependDept,DependDate,SummaryDate,SummaryDept) values(" & ParentId & ",'" & AreaCode & "','" & AreaName & "','" & AreaNum & "','" & AreaAddress & "','" & AreaDistance & "','" & FullPath & "','" & DependDept & "','" & DependDate & "','" & SummaryDate & "','" & SummaryDept & "');"
                sql += " SELECT SCOPE_IDENTITY()  as ID"
                Dim IdObject As Object = Exec(sql, E_ExecType.ExecuteScalar)
                ID = Val(IdObject & "")
            Else

                sql = "Update BSC_AreaSet Set AreaCode='" & AreaCode & "',AreaName='" & AreaName & "',AreaNum='" & AreaNum & "',AreaAddress='" & AreaAddress & "',AreaDistance='" & AreaDistance & "',FullPath='" & FullPath & "',DependDept='" & DependDept & "',DependDate='" & DependDate & "',SummaryDate='" & SummaryDate & "',SummaryDept='" & SummaryDept & "'    where ID=" & SelectNode.Tag
                Exec(sql, E_ExecType.ExecuteNonQuery)
            End If
            UpdateData_AreaSet(ID, ParentId, AreaCode, AreaName, Val(AreaNum), AreaAddress, AreaDistance, FullPath, DependDept, DependDate, SummaryDate)
            UpdateTreeView(Tvw_Area.SelectedNode, ID, AreaName)

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                MessageBox.Show(My.Resources.AddSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show(My.resources.EditSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            Me.SetButton(ButtonType.Browse, True)
            Me.SetEnabled_Tab1(False)

        Catch ex As Exception
            MessageBox.Show("SaveDataAreaSet:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Bool = False
        End Try
        Return Bool
    End Function

    ''' <summary>
    ''' 區域保存驗證
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Validated_AreaSet() As Boolean

        Dim sql As String = ""
        Dim Ds As New DataSet

        If Me.AreaCode.Text.Length <= 0 Then
            MsgBox(My.Resources.NotNullfCode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.AreaCode.Focus()
            Return False
        End If

        If Me.AreaName.Text.Length <= 0 Then
            MsgBox(My.Resources.regionalnamenull, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.AreaName.Focus()
            Return False
        End If
        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            sql = "select * from BSC_AreaSet where AreaCode='" & Me.AreaCode.Text & "'"

            Ds = ReturnDataset(sql)
            If Ds.Tables(0).Rows.Count > 0 Then
                MsgBox(My.Resources.ExisedtSameCode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.AreaCode.Focus()
                Me.AreaCode.SelectAll()
                Return False
            End If
        Else
            sql = "select * from dbo.BSC_AreaSet where Id<>" & Val(Me.Tvw_Area.SelectedNode.Tag & "") & " and AreaCode='" & Me.AreaCode.Text & "'" & vbCrLf
            sql &= " select * from dbo.BSC_AreaSet where Id=" & Val(Me.Tvw_Area.SelectedNode.Tag & "") & ""
            Ds = ReturnDataset(sql)
            If Ds.Tables(0).Rows.Count > 0 Then
                MsgBox(My.Resources.ExistsameCode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                AreaCode.Focus()
                Me.AreaCode.SelectAll()
                Return False
            End If

            If Ds.Tables(1).Rows.Count <= 0 Then
                MsgBox(My.Resources.unsave, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                AreaCode.Focus()
                Me.AreaCode.SelectAll()
                Return False
            End If

        End If

        Return True
    End Function

    ''' <summary>
    ''' 更新到數據集
    ''' </summary>
    ''' <param name="Id">自動增長列</param>
    ''' <param name="ParentID">父結點</param>
    ''' <param name="AreaCode">區域編號</param>
    ''' <param name="AreaName">區域名稱</param>
    ''' <param name="AreaNum">序號</param>
    ''' <param name="AreaAddress">地址</param>
    ''' <param name="AreaDistance">距離</param>
    ''' <param name="FullPath">路徑</param>
    ''' <param name="DependDept">部門</param>
    ''' <param name="DependDate">日期</param>
    ''' <param name="SummaryDate">備注</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function UpdateData_AreaSet(ByVal Id As Integer, ByVal ParentID As String, ByVal AreaCode As String, _
                                        ByVal AreaName As String, ByVal AreaNum As Integer, ByVal AreaAddress As String, ByVal AreaDistance As String, _
                                        ByVal FullPath As String, ByVal DependDept As String, ByVal DependDate As String, ByVal SummaryDate As String) As Boolean

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            Dim NewRow As DataRow = Me.Dtab_Area.NewRow
            NewRow("ID") = Id
            NewRow("ParentID") = ParentID
            NewRow("AreaCode") = AreaCode
            NewRow("AreaName") = AreaName
            NewRow("AreaNum") = AreaNum
            NewRow("AreaAddress") = AreaAddress
            NewRow("AreaDistance") = AreaDistance
            NewRow("FullPath") = FullPath

            NewRow("DependDept") = DependDept
            NewRow("DependDate") = DependDate
            NewRow("SummaryDate") = SummaryDate
            Dtab_Area.Rows.Add(NewRow)

        Else
            Dim DRow() As DataRow = Me.Dtab_Area.Select("Id=" & Id & "")
            If DRow.Length > 0 Then

                DRow(0).Item("AreaCode") = AreaCode
                DRow(0).Item("AreaName") = AreaName
                DRow(0).Item("AreaNum") = AreaNum
                DRow(0).Item("AreaAddress") = AreaAddress
                DRow(0).Item("AreaDistance") = AreaDistance
                DRow(0).Item("FullPath") = FullPath

                DRow(0).Item("DependDept") = DependDept
                DRow(0).Item("DependDate") = DependDate
                DRow(0).Item("SummaryDate") = SummaryDate

            End If
        End If

        Dtab_Area.AcceptChanges()

        Return True
    End Function

    ''' <summary>
    ''' 更新樹結點
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function UpdateTreeView(ByVal SelectedNode As TreeNode, ByVal Id As Integer, ByVal AreaName As String) As Boolean
        Try
            Select Case ActionHelp

                Case IDServer.IDServer.ActionBtn.ActionAdd

                    Dim TempNode As TreeNode = SelectedNode.Nodes.Add(AreaName)
                    TempNode.Tag = Id
                    LoadNodeImage(TempNode, SelectedNode.Level + 1)
                    Dim TempAction As IDServer.IDServer.ActionBtn = ActionHelp

                    ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
                    fLoodBool = False
                    Me.Tvw_Area.SelectedNode = TempNode
                    fLoodBool = True
                    ActionHelp = TempAction

                Case IDServer.IDServer.ActionBtn.ActionEdit
                    SelectedNode.Text = AreaName
            End Select
        Catch ex As Exception
            MessageBox.Show("UpdateTreeView:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

#End Region

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetToolEnable()
        Try
            With MainForm
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable
                .ToolStripDelete.Enabled = DeleteEnable
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("TreeView1_AfterSelect:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
#End Region

#Region "Controls Events"

#Region "Form Load"

    Private Sub Frm_Truct_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.fLoodBool = False
            Initialize()
            Me.fLoodBool = True
        Catch ex As Exception
            MessageBox.Show("Frm_Truct_Load:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "TreeView Events"

    Private Sub Tvw_1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles Tvw_1.AfterSelect, Tvw_Area.AfterSelect
        Try
            If Me.fLoodBool = True Then
                Dim Tvw As TreeView = DirectCast(sender, TreeView)
                Select Case Tvw.Name.ToLower
                    Case Me.Tvw_1.Name.ToLower
                        SetButton(ButtonType.Browse, True)
                        FillDetail(e.Node)
                    Case Me.Tvw_Area.Name.ToLower
                        SetButton(ButtonType.Browse, True)
                        FillDetialAreaSet(e.Node)
                        GetToolEnable()
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show("Tvw_1_AfterSelect:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    ''' <summary>
    ''' 結點展開前
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Tvw_1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles Tvw_1.BeforeExpand, Tvw_Area.BeforeExpand
        Try
            Dim Tvw As TreeView = DirectCast(sender, TreeView)
            Select Case Tvw.Name.ToLower
                Case Me.Tvw_1.Name.ToLower
                    Me.Load_TreeNode(e.Node)
                Case Me.Tvw_Area.Name.ToLower
                    Me.Load_TreeNodeForAreaSet(e.Node)
            End Select
        Catch ex As Exception
            MessageBox.Show("Tvw_1_BeforeExpand:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    ''' <summary>
    ''' 結點展開後
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Tvw_1_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles Tvw_1.BeforeSelect, Tvw_Area.BeforeSelect
        Try
            If ActionHelp <> IDServer.IDServer.ActionBtn.ActionHelp Then
                e.Cancel = True
                Exit Sub
            End If
            Dim Tvw As TreeView = DirectCast(sender, TreeView)

            If e.Node Is Nothing Then Exit Sub
            Dim Node As TreeNode = Tvw.SelectedNode
            If Not Node Is Nothing Then
                Node.ForeColor = Color.Black
            End If
            e.Node.ForeColor = Color.Blue
        Catch ex As Exception
            MessageBox.Show("Tvw_1_BeforeSelect:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    ''' <summary>
    ''' TreeView DoubleClick
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TreeView2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView2.DoubleClick, TreeView3.DoubleClick
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                Dim Tree As TreeView = DirectCast(sender, TreeView)
                Dim Node As TreeNode = Tree.SelectedNode
                If Not Node Is Nothing Then
                    Tree.Nodes.Remove(Node)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("TreeView2_DoubleClick:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Sub

#End Region

#Region "TabControl"

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Me.SetButton(ButtonType.Browse, True)
    End Sub

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
            e.Cancel = True
        End If
    End Sub

#End Region

#Region "Value Changed Events"

    ''' <summary>
    ''' Text Changed Event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cbxDependDept_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxDependDept.TextChanged, cbxDependDate.TextChanged
        Try
            If Me.fLoodBool = True AndAlso (ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit) Then
                Dim Cbx As ComboBox = DirectCast(sender, ComboBox)
                Select Case Cbx.Name.ToLower

                    Case Me.cbxDependDept.Name.ToLower

                        If Cbx.Text.Trim <> "" AndAlso Not Cbx.SelectedValue Is Nothing Then
                            Dim fCode As String = Cbx.SelectedValue
                            Dim fName As String = Cbx.Text.Trim
                            Dim Node As TreeNode
                            If Me.TreeView2.Nodes.ContainsKey(fCode) = False Then
                                Node = Me.TreeView2.Nodes.Add(fCode, fName)
                                Node.Tag = fCode
                            End If
                        End If

                    Case Me.cbxDependDate.Name.ToLower

                        If Cbx.Text.Trim <> "" AndAlso [Enum].IsDefined(GetType(E_WeekDay), Cbx.Text.Trim) = True Then
                            Dim fName As String = Cbx.Text.Trim
                            Dim fCode As String = [Enum].Parse(GetType(E_WeekDay), fName)
                            If Me.TreeView3.Nodes.ContainsKey(fCode) = False Then
                                Dim Node As TreeNode = Me.TreeView3.Nodes.Add(fCode, fName)
                                Node.Tag = fCode
                            End If
                        End If

                End Select
            End If
        Catch ex As Exception
            MessageBox.Show("cbxDependDept_TextChanged" + ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region


#End Region

#Region "Inteface Define"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show(My.resources.NotAllowAdd, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Dim Node As TreeNode = Nothing
            Select Case Me.TabControl1.SelectedIndex
                Case 0

                    ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
                    Me.FillDetialAreaSet(Nothing)
                    Me.SetButton(ButtonType.Add, False)
                    SetEnabled_Tab1(True)
                    Me.AreaCode.Focus()
                    Return True
                Case 1

                    Node = Me.Tvw_1.SelectedNode
                    If Node Is Nothing OrElse Node.Level = 0 Then
                        MainForm.ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
                        Return False
                    End If
                    ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
                    Me.SetButton(ButtonType.Add, False)
                    Me.SetEnabled(True)
                    Me.txtTruckId.Focus()
                    Me.txtTruckId.SelectAll()
                    Return True

            End Select
        Catch ex As Exception
            MessageBox.Show("ActionAdd:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try

            Select Case Me.TabControl1.SelectedIndex
                Case 0
                    Dim Node As TreeNode = Me.Tvw_Area.SelectedNode
                    Me.FillDetialAreaSet(Node)
                    Me.SetButton(ButtonType.Browse, True)
                    SetEnabled_Tab1(False)
                    ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
                Case 1
                    Me.SetEnabled(False)

                    Me.SetButton(ButtonType.Browse, True)
                    Dim Node As TreeNode = Me.Tvw_1.SelectedNode
                    Me.FillDetail(Node)
                    ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            End Select
            Return True
        Catch ex As Exception
            MessageBox.Show("ActionCancel:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show(My.resources.NotAllowdelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Dim Node As TreeNode = Nothing

            Select Case Me.TabControl1.SelectedIndex
                Case 0
                    Delete_TreeNode_AreaSet(Me.Tvw_Area, Me.Tvw_Area.SelectedNode)
                Case 1
                    Node = Me.Tvw_1.SelectedNode
                    If Node.Level <> 2 Then Return False

                    If MessageBox.Show(My.Resources.ConfirmDelete, Msg.CompanyName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) <> DialogResult.OK Then Return False

                    Dim Sql As String = ""
                    Dim ParentId As String = ""
                    Dim TruckId As String = ""

                    ParentId = Node.Tag.ToString.Split(":")(0).Trim
                    TruckId = Node.Tag.ToString.Split(":")(1).Trim

                    Sql &= " Delete BSC_Truck where TruckId='" & TruckId & "' and ParentId='" & ParentId & "'" & vbCrLf

                    Exec(Sql, E_ExecType.ExecuteNonQuery)

                    Dim DRow() As DataRow = Me.Dtab_Truck.Select("Isnull(TruckId,'')='" & TruckId & "'  and Isnull(ParentId,'')='" & ParentId & "'")
                    For Each Row As DataRow In DRow
                        Row.Delete()
                    Next
                    Me.Dtab_Truck.AcceptChanges()

                    Dim ParentNode As TreeNode = Node.Parent
                    Dim PrevNode As TreeNode = Node.PrevNode
                    Dim NextNode As TreeNode = Node.NextNode

                    Node.Remove()

                    If Not NextNode Is Nothing Then
                        Me.Tvw_1.SelectedNode = NextNode
                    ElseIf Not PrevNode Is Nothing Then
                        Me.Tvw_1.SelectedNode = PrevNode
                    Else
                        Me.Tvw_1.SelectedNode = ParentNode
                    End If

                    MessageBox.Show(My.resources.DeleteSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)

            End Select
            Return True
        Catch ex As Exception
            MessageBox.Show("ActionDelete:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try

            If EditEnable = False Then
                MessageBox.Show(My.resources.NotAllowEidt, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Select Case Me.TabControl1.SelectedIndex
                Case 0
                    ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
                    Me.SetButton(ButtonType.Edit, False)
                    Me.SetEnabled_Tab1(True)

                Case 1
                    ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
                    Me.SetButton(ButtonType.Edit, False)
                    Me.SetEnabled(True)
                    Me.txtTruckId.ReadOnly = True
                    Me.txtTruckName.Focus()
                    Me.txtTruckName.SelectAll()
            End Select

            Return True

        Catch ex As Exception
            MessageBox.Show("ActionEdit:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        Dim bool As Boolean = False
        Select Case Me.TabControl1.SelectedIndex
            Case 0
                bool = SaveDataAreaSet()
            Case 1
                bool = btnSaveData()
        End Select

        Return bool
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region


    Private Sub AreaCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AreaCode.TextChanged

    End Sub
End Class
